package core;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Point;
import java.util.Vector;

import core.actions.*;





public class V2Spinner {

/*	final static int size=120, iSize=24, barSize=10, barArcSize=70;

	final static double PI = 3.1415;*/
	private V2SpinnerGraphics myGraphics;
	private boolean multiClickSupport=true;
	Player master;
	protected ActionMap<Action> actions;
	int frame;
	protected double optionDir;
	int speed=3;
	int currentOption, currentDepth;
	private DirectionChoice dirChoice;
	private PositionChoice posChoice;
	
	private int dcTicker;
	private int dcFrames = 35;
	private int dcCount;
	
	
	public V2Spinner(ActionMap<Action> act, Player m){
		actions=act;
		master=m;
		myGraphics=new V2SpinnerGraphics(this);
	}
	public V2Spinner(ActionMap<Action> act, Color c){
		actions=act;
		master=null;
		myGraphics=new V2SpinnerGraphics(this, c);
	}
	
	

	public void click(){
		dcCount++;		
		if(multiClickSupport){
			currentDepth=dcCount-1;
			dcTicker=dcFrames;
		}
		else
			activate();
	}
	
	private Action currentAction(){
		return actions.getAction(currentOption, currentDepth);
	}
	
	public Action getAction(){
		return currentAction();
	}
	
	public void nextFrame(){  // moves everything, activates after doubleclick expires
		if(dirChoice!=null){
			dirChoice.nextFrame();
		}
		else if(posChoice!=null){
			posChoice.nextFrame();
		}
		else if(dcTicker>0){
			dcTicker--;
			if(dcTicker==0){
				currentDepth=dcCount-1;
				activate();
			}
		}		
		else{
			spin();
		}
		
			
	}
	private void activate(){
		if(currentAction()!=null){		
			if(currentAction().isReady()){
				currentAction().activate();
				currentDepth=0;
			}
			else if(currentAction().hasEnoughEnergy()){
				//System.out.println("Rad: "+currentOption+"\nDepth: "+currentDepth);
				if(dirChoice!=null){
					currentAction().setInputDir(dirChoice.getDir());
					dirChoice=null;
				}
				if(posChoice!=null){
					currentAction().setInputPoint(posChoice.getPoint());
					posChoice=null;
				}
				
				if(currentAction().needDir()){
					
					dirChoice=currentAction().getDirChoice();
					
				}
				else if(currentAction().needPos()){
					posChoice=currentAction().getPosChoice();
				}
				if(currentAction().isReady()){
					currentAction().activate();
					currentDepth=0;
				}
			}
			
		}
		else{
			currentDepth=0;
		}
		
		updateMultiClickEnabled();
		dcCount=0;
		dcTicker=0;
		myGraphics.generatePaths();
	}
	private void spin(){
		frame++;
		optionDir=optionDir+0.002*speed;
		if(optionDir>=1) {
			optionDir--;
		}
		if(actions.getRadius() != 0)
			currentOption=(int)(optionDir*actions.getRadius());
	}
	private void updateMultiClickEnabled(){
		if(dirChoice == null && posChoice == null)
			multiClickSupport = true;
		else
			multiClickSupport = false;
	}
	
	/*public void paint(Graphics g) {

		myGraphics.paint(g);
		
		
    	if(dirChoice!=null)
    		dirChoice.paint(g);
    	if(posChoice!=null)
    		posChoice.paint(g);	
	}*/
	public void paint(Point screenOffset, int zoomDivider, Graphics g){
		myGraphics.paint(g);
		
    	if(dirChoice!=null)
    		dirChoice.paint(screenOffset, zoomDivider, g);
    	if(posChoice!=null)
    		posChoice.paint(screenOffset, zoomDivider, g);	
	}
	public void setPosition(Point p){
		myGraphics.setPosition(p);
	}
	public void refreshGraphics() {
		myGraphics.generatePaths();
	}
}
